AddCSLuaFile("cl_init.lua")

AddCSLuaFile("shared.lua")

include("shared.lua")

seedType = seedType or 1

function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("weed_plant") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 50)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it
 
	return ent -- You need to return the entity to make it work
end

function ENT:Initialize()
 
	self.Entity:SetModel("models/nater/weedplant_pot_dirt.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   -- after all, gmod is a physics
	self.Entity:SetSolid( SOLID_VPHYSICS )         -- Toolbox

	self.Entity:PhysicsInit(SOLID_VPHYSICS)

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
			phys:Wake()
		end
	 
	self.Entity:SetNetworkedBool("Usable", false)

	self.Entity:SetNetworkedBool("Plantable", true)

	self.damage = 10

	local ply = self.Entity:GetNetworkedEntity("owning_ent")

end

function ENT:OnTakeDamage(dmg)

	self.damage = self.damage - dmg:GetDamage()

	if (self.damage <= 0) then
		local effectdata = EffectData()
		effectdata:SetOrigin(self.Entity:GetPos())
		effectdata:SetMagnitude(2)
		effectdata:SetScale(2)
		effectdata:SetRadius(3)
		util.Effect("Sparks", effectdata)
		self.Entity:Remove()
	end

end

function ENT:Use()
	
	if self.Entity:GetNetworkedBool("Usable") == true then
		
		for i=1, seedType do
			local SpawnPos = self.Entity:GetPos() + self.Entity:GetAngles():Up()*(18+(3*i))
			local WeedBag = ents.Create("durgz_weed")
			WeedBag:SetPos(SpawnPos)
			WeedBag:Spawn()
		end
	

		self.Entity:SetNetworkedBool("Plantable", true)
		self.Entity:SetNetworkedBool("Usable", false)
		self.Entity:SetModel("models/nater/weedplant_pot_dirt.mdl")

	end

end

local function startGrowing(ent, mod)

	timer.Create("Stage2_"..ent:EntIndex(), 2/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing1.mdl")
	end)

	timer.Create("Stage3_"..ent:EntIndex(), 6/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing2.mdl")
	end)

	timer.Create("Stage4_"..ent:EntIndex(), 10/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing3.mdl")
	end)

	timer.Create("Stage5_"..ent:EntIndex(), 25/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing4.mdl")
	end)

	timer.Create("Stage6_"..ent:EntIndex(), 30/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing5.mdl")
	end)

	timer.Create("Stage7_"..ent:EntIndex(), 40/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing6.mdl")
	end)

	timer.Create("Stage8_"..ent:EntIndex(), 55/mod, 1, function()
		ent:SetModel("models/nater/weedplant_pot_growing7.mdl")
		ent:SetNetworkedBool("Usable", true)
	end)
end

function ENT:Touch(hitEnt)
	if self.Entity:GetNetworkedBool("Plantable") == true then

		if hitEnt:GetClass() == "seed_weed" then
			seedType = 1
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,1)
			hitEnt:Remove()
		end
		if hitEnt:GetClass() == "ruby_seed_weed" then
			seedType = 1
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,1.2)
			hitEnt:Remove()
		end
		if hitEnt:GetClass() == "gold_seed_weed" then
			seedType = 2
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,1.8)
			hitEnt:Remove()
		end
		if hitEnt:GetClass() == "radioactive_seed_weed" then
			seedType = 3
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,2.2)
			hitEnt:Remove()
		end
		
	end
end

function ENT:Planterplant(seed)
	if self.Entity:GetNetworkedBool("Plantable") == true then

		if seed == "seed_weed" then
			seedType = 1
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,1)
		end
		if seed == "ruby_seed_weed" then
			seedType = 1
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,1.2)
		end
		if seed == "gold_seed_weed" then
			seedType = 2
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,1.8)
		end
		if seed == "radioactive_seed_weed" then
			seedType = 2
			self.Entity:SetNetworkedBool("Plantable", false)
			self.Entity:SetModel("models/nater/weedplant_pot_planted.mdl")
			startGrowing(self,2.2)
		end
		
	end
end

function ENT:OnRemove()
	if self.Entity:GetNetworkedBool("Plantable") == false then
		timer.Destroy("Stage2")
		timer.Destroy("Stage3")
		timer.Destroy("Stage4")
		timer.Destroy("Stage5")
		timer.Destroy("Stage6")
		timer.Destroy("Stage7")
		timer.Destroy("Stage8")
	end
end 